package org.apache.ibatis.sql.executor;

/**
 * @Name ErrorContext
 * @Description: 异常上下文信息类
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-04-24 16:13:02
 **/
public class ErrorContext {
    // 获取当前操作系统的换行符
    private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
    // 将自身存储到 ThreadLocal 中，从而实现线程间的隔离
    private static final ThreadLocal<ErrorContext> LOCAL = new ThreadLocal<>();
    // 存储上级异常，从而组成异常链
    private ErrorContext stored;

    // 异常信息，可以写入一项或者多项
    private String message;     // 异常消息
    private String resource;    // 异常所在资源
    private String object;      // 异常对象
    private String activity;    // 异常活动范围
    private String sql;         // 异常 SQL 语句
    private Throwable cause;    // 异常原因

    private ErrorContext() {
    }

    /**
     * 从 ThreadLocal 中取出已经实例化的异常上下文，或者创建一个异常上下文并放入 ThreadLocal 中
     *
     * @return org.apache.ibatis.sql.executor.ErrorContext 异常上下文
     * @version v1.0.0
     * @author lichao
     * @date 2024-04-24 16:29:27
     */
    public static ErrorContext instance() {
        ErrorContext context = LOCAL.get();
        if (context == null) {
            context = new ErrorContext();
            LOCAL.set(context);
        }
        return context;
    }

    /**
     * 创建一个包含上级异常上下文的新异常上下文
     *
     * @return org.apache.ibatis.sql.executor.ErrorContext 新异常上下文
     * @version v1.0.0
     * @author lichao
     * @date 2024-04-24 16:30:48
     */
    public ErrorContext store() {
        ErrorContext newContext = new ErrorContext();
        newContext.stored = this;
        LOCAL.set(newContext);
        return LOCAL.get();
    }

    /**
     * 获取剥离出当前异常上下文的内部异常上下文
     *
     * @return org.apache.ibatis.sql.executor.ErrorContext 内部异常上下文
     * @version v1.0.0
     * @author lichao
     * @date 2024-04-24 16:32:00
     */
    public ErrorContext recall() {
        if (stored != null) {
            LOCAL.set(stored);
            stored = null;
        }
        return LOCAL.get();
    }

    public ErrorContext resource(String resource) {
        this.resource = resource;
        return this;
    }

    public ErrorContext activity(String activity) {
        this.activity = activity;
        return this;
    }

    public ErrorContext object(String object) {
        this.object = object;
        return this;
    }

    public ErrorContext message(String message) {
        this.message = message;
        return this;
    }

    public ErrorContext sql(String sql) {
        this.sql = sql;
        return this;
    }

    public ErrorContext cause(Throwable cause) {
        this.cause = cause;
        return this;
    }

    public ErrorContext reset() {
        this.resource = null;
        this.activity = null;
        this.object = null;
        this.message = null;
        this.sql = null;
        this.cause = null;
        LOCAL.remove();
        return this;
    }

    @Override
    public String toString() {
        StringBuilder description = new StringBuilder();
        if (this.message != null) {
            description.append(LINE_SEPARATOR);
            description.append("### ");
            description.append(this.message);
        }
        if (this.resource != null) {
            description.append(LINE_SEPARATOR);
            description.append("### 当前异常可能存在于：");
            description.append(this.resource);
        }
        if (this.object != null) {
            description.append(LINE_SEPARATOR);
            description.append("### 当前异常可能涉及：");
            description.append(this.object);
        }
        if (this.activity != null) {
            description.append(LINE_SEPARATOR);
            description.append("### 当前异常可能发生在：");
            description.append(this.activity);
        }
        if (this.sql != null) {
            description.append(LINE_SEPARATOR);
            description.append("### SQL 语句：");
            description.append(this.sql.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').trim());
        }
        if (this.cause != null) {
            description.append(LINE_SEPARATOR);
            description.append("### 异常原因：");
            description.append(this.cause.toString());
        }
        return description.toString();
    }
}

